package com.rms.recruit_management_system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rms.recruit_management_system.mapper.InterviewVoMapper;
import com.rms.recruit_management_system.pojo.Interview;
import com.rms.recruit_management_system.pojo.Positions;
import com.rms.recruit_management_system.service.InterviewService;
import com.rms.recruit_management_system.utils.Result;
import com.rms.recruit_management_system.vo.InterviewVo;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class InterviewController {
    @Autowired
    private InterviewService interviewService;
    @Autowired
    private InterviewVoMapper interviewVoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/interview/list")
    @Operation(summary = "获取所有面试信息",description = "获取所有面试信息")
    public Result<?> getInterview(@RequestParam(defaultValue = "1") Integer current,
                                  @RequestParam(defaultValue = "10") Integer size,
                                  @RequestParam(defaultValue = "") String search)   {
        // 改进键的生成，添加分隔符
        String key = "interview_" + current + "_" + size + "_" + search;

        // 从 Redis 获取数据
        IPage<Interview> page = (IPage<Interview>) redisTemplate.opsForValue().get(key);

        if (page!= null && page.getTotal() > 0) {
            log.info("redis", page);
            return new Result<>(200, "SUCCESS", page);
        } else {
            // 从数据库获取数据
            page = interviewService.getInterview(current, size, search);
            log.info("mysql", page);
            // 存储数据到 Redis，并设置过期时间
            redisTemplate.opsForValue().set(key, page, 60, TimeUnit.SECONDS);
            return new Result<>(200, "success", page);
        }

    }
    @Operation(summary = "添加面试信息",description = "添加面试信息")
    @RequestMapping("/interview/add")
    public Result<?> addInterview(@RequestBody Interview interview)  {
        interviewService.addInterview(interview);
        cleanCache("interview_*");
        return new Result<>(200, "success");
    }
    @Operation(summary = "修改面试信息",description = "根据id修改面试信息")
    @RequestMapping("/interview/updateStatus")
    public Result<?> updateInterviewStatus(@RequestBody Interview interview)  {
        interviewService.updateInterviewStatus(interview);
        cleanCache("interview_*");
        return new Result<>(200, "success");
    }

    @Operation(summary = "删除面试信息",description = "根据id删除面试信息")
    @RequestMapping("/interview/delete/{id}")
    public Result<?> deleteInterview(@PathVariable Integer id)  {
        interviewService.deleteInterview(id);
        cleanCache("interview_*");
        return new Result<>(200, "success");
    }
    @Operation(summary = "检验是否存有面试记录",description = "根据id检查是否存有面试记录")
    @RequestMapping("/interview/checkIsRecords/{id}")
    public Result<?> checkIsRecords(@PathVariable Integer id)  {
        InterviewVo interviewVo=interviewVoMapper.selectInterviewVoById(id);
        cleanCache("interview_*");
        return new Result<>(200, "success", interviewVo);
    }

    /**
     * 清空缓存
     * @param pattern
     */
    public void cleanCache(String pattern){
        Set keys=redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
